package question_answer

import (
	"errors"
	"news_for_deepseek_interface/config"
	questionAnswerDto "news_for_deepseek_interface/dto/admin/question_answer"
	"news_for_deepseek_interface/model"
	tencentlke "news_for_deepseek_interface/project/tencent/lke"

	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	tencenterrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	lke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lke/v20231130"
)

func Delete(params questionAnswerDto.DeleteRequest) error {

	for _, v := range params.Id {
		var questionAnswer model.QuestionAnswerModel
		db := model.GetInstance().GetDB()
		if err := db.Where("id = ?", v).First(&questionAnswer).Error; err != nil {
			return errors.New("问答不存在")
		}

		// 实例化一个请求对象,每个接口都会对应一个request对象
		request := lke.NewDeleteQARequest()

		request.BotBizId = common.StringPtr(config.DefaultConfig.Tencent.Lke.BotBizId)
		request.QaBizIds = common.StringPtrs([]string{questionAnswer.QrBizId})

		// 返回的resp是一个DeleteQACateResponse的实例，与请求对象对应
		_, err := tencentlke.LkeClient.DeleteQA(request)
		if _, ok := err.(*tencenterrors.TencentCloudSDKError); ok {
			//	return errors.New("腾讯云接口删除问答失败:" + err.Error())
		}
		if err != nil {
			//	return errors.New("腾讯云接口删除问答失败1:" + err.Error())
		}

		db.Where("id = ?", v).Delete(&model.QuestionAnswerModel{})
	}

	return nil
}
